package com.rykj.energy.database.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rykj.energy.common.utils.StringUtils;
import com.rykj.energy.database.mapper.MsgResultMapper;
import com.rykj.energy.database.pojo.MessageResult;
import com.rykj.energy.database.pojo.Organization;
import com.rykj.energy.database.pojo.vo.MsgParamVo;
import com.rykj.energy.database.pojo.vo.MsgResultVo;
import com.rykj.energy.database.service.IMsgResultService;
import com.rykj.energy.database.service.IOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service("msgResultService")
public class MsgResultServiceImpl implements IMsgResultService {

    @Autowired
    private MsgResultMapper msgResultMapper;
    @Autowired
    private IOrganizationService orgService;

    @Override
    public  IPage<MsgResultVo> findMessages(Integer pageNum, Integer pageSize, MsgParamVo msgParams) {
        List<Integer> orgIds =new ArrayList<>();
        Integer orgId =msgParams.getOrgId();
        String orgNo=msgParams.getOrgNo();
        if(StringUtils.isNotEmpty(orgNo)){
           Organization org= orgService.findByOrgNo(orgNo);
           orgId=org.getId();
        }
        String orgName =msgParams.getOrgName();
        String cameraIds=msgParams.getCameraId();
        List<String> camIds =new ArrayList<>();
        if(StringUtils.isNotEmpty(cameraIds)){
            camIds = Arrays.asList(cameraIds.split(","));
        }
        String businessCode =msgParams.getBusinessCode();
        List<String> msgTypes =new ArrayList<>();
        if(StringUtils.isNotEmpty(businessCode)){
            msgTypes = Arrays.asList(businessCode.split(","));
        }

        String msgLevel =msgParams.getMsgLevel();
        String dealState =msgParams.getDealState();
        String endTime =msgParams.getEndTime();
        String startTime =msgParams.getStartTime();

        if(StringUtils.isNotEmpty(orgId)&& !"0".equals(orgId+"") ){
            orgIds= orgService.getChildOrgIdsById(msgParams.getOrgId());
            orgIds.add(msgParams.getOrgId());
        }
        Page<MsgResultVo> page=new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        IPage<MsgResultVo> msgPag= msgResultMapper.findMessages(page,orgIds,camIds,msgTypes,orgName,msgLevel,dealState,startTime,endTime);
        return msgPag;
    }

    @Override
    public void saveMsgResult(MessageResult msgResult) {
        msgResultMapper.insert(msgResult);
    }

    @Override
    public IPage<Map<String, Object>> selectImgsByDate(Page<Map<String, Object>> page, String delDate) {
        return msgResultMapper.selectImgsByDate(page,delDate);
    }

    @Override
    public MessageResult selectById(Integer resultId) {
        return msgResultMapper.selectById(resultId);
    }

    @Override
    public void updateById(MessageResult msgResult) {
        msgResultMapper.updateById(msgResult);
    }

    @Override
    public void updateDealStateById(String[] ids, String dealRemark) {
        msgResultMapper.updateDealStateById(ids,dealRemark);
    }

    @Override
    public void deleteById(Integer id) {
        msgResultMapper.deleteById(id);
    }
}
